package com.zbkj.service.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zbkj.common.model.activity.SfullDiscountActivity;
import com.zbkj.common.model.activity.SfullDiscountProduct;
import com.zbkj.common.model.product.Product;
import com.zbkj.common.page.CommonPage;
import com.zbkj.common.request.PageParamRequest;
import com.zbkj.common.request.merchant.MerchantSfullDiscountProductSearchRequest;
import com.zbkj.service.dao.MerchantSfullDiscountProductDao;
import com.zbkj.service.service.MerchantSfullDiscountProductService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * MerchantSfullDiscountProductServiceImpl 接口实现
 * +----------------------------------------------------------------------
 * | CRMEB [ CRMEB赋能开发者，助力企业发展 ]
 * +----------------------------------------------------------------------
 * | Copyright (c) 2016~2023 https://www.crmeb.com All rights reserved.
 * +----------------------------------------------------------------------
 * | Licensed CRMEB并不是自由软件，未经许可不能去掉CRMEB相关版权
 * +----------------------------------------------------------------------
 * | Author: CRMEB Team <admin@crmeb.com>
 * +----------------------------------------------------------------------
 */
@Service
public class MerchantSfullDiscountProductServiceImpl extends ServiceImpl<MerchantSfullDiscountProductDao, SfullDiscountProduct> implements MerchantSfullDiscountProductService {

    @Resource
    private MerchantSfullDiscountProductDao dao;

    /**
     * 商户满减折扣商品列表
     * @param request 请求参数
     * @return List
     */
    @Override
    public PageInfo<SfullDiscountProduct> findPage(MerchantSfullDiscountProductSearchRequest request, PageParamRequest pageParamRequest) {
        LambdaQueryWrapper<SfullDiscountProduct> lqw = Wrappers.lambdaQuery();
        lqw.eq(Objects.nonNull(request.getProductId()), SfullDiscountProduct::getProductId, request.getProductId());
        lqw.eq(StringUtils.isNotBlank(request.getName()), SfullDiscountProduct::getName, request.getName());
        lqw.eq(Objects.nonNull(request.getSfullDiscountId()), SfullDiscountProduct::getSfullDiscountId, request.getSfullDiscountId());
        lqw.in(Objects.nonNull(request.getProductIds()), SfullDiscountProduct::getProductId, request.getProductIds());
        lqw.eq(SfullDiscountProduct::getIsDel, false);
        List<SfullDiscountProduct> activityList = dao.selectList(lqw);
        Page<Product> page = PageHelper.startPage(pageParamRequest.getPage(), pageParamRequest.getLimit());
        return CommonPage.copyPageInfo(page, activityList);
    }

    @Override
    public List<SfullDiscountProduct> findAllList(MerchantSfullDiscountProductSearchRequest request){
        LambdaQueryWrapper<SfullDiscountProduct> lqw = Wrappers.lambdaQuery();
        lqw.eq(Objects.nonNull(request.getProductId()), SfullDiscountProduct::getProductId, request.getProductId());
        lqw.eq(StringUtils.isNotBlank(request.getName()), SfullDiscountProduct::getName, request.getName());
        lqw.eq(Objects.nonNull(request.getSfullDiscountId()), SfullDiscountProduct::getSfullDiscountId, request.getSfullDiscountId());
        lqw.in(Objects.nonNull(request.getProductIds()), SfullDiscountProduct::getProductId, request.getProductIds());
        lqw.eq(SfullDiscountProduct::getIsDel, false);
        return dao.selectList(lqw);
    }

    @Override
    public SfullDiscountProduct findOne(MerchantSfullDiscountProductSearchRequest request){
        LambdaQueryWrapper<SfullDiscountProduct> lqw = Wrappers.lambdaQuery();
        lqw.eq(Objects.nonNull(request.getProductId()), SfullDiscountProduct::getProductId, request.getProductId());
        lqw.eq(Objects.nonNull(request.getSfullDiscountId()), SfullDiscountProduct::getSfullDiscountId, request.getSfullDiscountId());
        lqw.eq(SfullDiscountProduct::getIsDel, false);
        return dao.selectOne(lqw);
    }

    /**
     * 新增商户满减折扣商品
     * @param productList 请求参数
     * @return 新增结果
     */
    @Override
    public Boolean addBatch(SfullDiscountActivity activity, List<SfullDiscountProduct> productList) {
        //先删除原有的关联商品订单数据
        dao.delete(new LambdaQueryWrapper<SfullDiscountProduct>()
                .eq(SfullDiscountProduct::getSfullDiscountId, activity.getId()));
        //再新增
        for (SfullDiscountProduct discountProduct : productList) {
            LambdaQueryWrapper<SfullDiscountProduct> lqw = Wrappers.lambdaQuery();
            lqw.eq(SfullDiscountProduct::getProductId, discountProduct.getProductId());
            lqw.eq(SfullDiscountProduct::getSfullDiscountId, activity.getId());
            lqw.eq(SfullDiscountProduct::getIsDel, false);
            if (Objects.isNull(dao.selectOne(lqw))){
                //没有就新增
                discountProduct.setSfullDiscountId(activity.getId());
                discountProduct.setSfullDiscountName(activity.getName());
                dao.insert(discountProduct);
            }
        }
        return true;
    }

    /**
     * 删除商户满减折扣商品
     * @param ids 商户满减折扣商品ID
     * @return 删除结果
     */
    @Override
    public Boolean delete(List<Integer> ids) {
        return dao.deleteBatchIds(ids)>0 ? true:false;
    }
}

